package com.bw.shop.splash;

import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.providerservice.CheckLoginApi;
import com.bw.network_sdk.BaseEntity;
import com.bw.user_module.user.UserEntity;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SplashViewModel extends BaseViewModel<SplashRepository> {
    public ObservableField<String>timeObservableField = new ObservableField<>("5s");
    private int time = 5;
    private TimeHandler handler = new TimeHandler(this);
    @Override
    public void onResult(BaseEntity entity) {
        //获取服务器返回值
        if (entity instanceof TokenEntity){
            //获取到token返回值
            SPUtils.getInstance().put("token",((TokenEntity) entity).getValues());
        }
        //获取用户信息缓存用户数据
        if (entity instanceof UserEntity){
            UserEntity userEntity = (UserEntity) entity;
            if (userEntity.message.equals("用户已登录")){
//                //获取到本地用户缓存数据
                SPUtils.getInstance().put("userid",userEntity.data.id);
            }
        }
    }

    @Override
    public void onComplete() {
        super.onComplete();
        new Thread(){
            @Override
            public void run() {
                super.run();
                for (int i = 0;i < 6;i++){
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.sendEmptyMessage(0);
                }
            }
        }.start();
    }

    @Override
    protected void initData() {
        //构建请求参数map
        Map<String,Object>requestMap = new HashMap<>();
        requestMap.put("imie",""+(System.currentTimeMillis()/1000));
        respository.requestToken(requestMap).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    public static class TimeHandler extends Handler{
        private WeakReference<SplashViewModel>weakReference;

        public TimeHandler(SplashViewModel viewModel){
            weakReference = new WeakReference<>(viewModel);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (weakReference.get()==null)
                return;
            weakReference.get().time-=1;
            if (weakReference.get().time<=0){
                //倒计时小于0->倒计时时长结束->跳转界面
                //pathArouter路径
                weakReference.get().startPage("/app/frameactivity");
                return;
            }
//            weakReference.get().time-=1;
            weakReference.get().timeObservableField.set(""+weakReference.get().time+"s");
        }
    }

}
